//
//  CustomTabbarItem.m
//  17LoanTemplate
//
//  Created by gynjobs on 2017/8/23.
//  Copyright © 2017年 cangming. All rights reserved.
//

#import "CustomTabbarItem.h"

@interface CustomTabbarItem()
@property(strong,nonatomic)UIButton *UpIconButton;
@property(strong,nonatomic)UILabel *DownTitleLable;
/** 点击手势 **/
@property(strong,nonatomic)UITapGestureRecognizer *tapGes;
@end

@implementation CustomTabbarItem

-(instancetype)init{
    self = [super init];
    if (self) {
        [self addSubWidghForSelf];
    }
    return self;
}
-(void)addSubWidghForSelf{
    self.UpIconButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.UpIconButton.backgroundColor = [UIColor clearColor];
    [self addSubview:self.UpIconButton];
    self.UpIconButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
    self.DownTitleLable = [[UILabel alloc]init];
    self.DownTitleLable.backgroundColor = [UIColor clearColor];
    [self addSubview:self.DownTitleLable];
    /** 添加手势 **/
    self.tapGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(ClickSelfView)];
    [self addGestureRecognizer:self.tapGes];
}
#pragma mark ---通过属性设置---
-(void)setUpImageName:(NSString *)upImageName{
    if (![_upImageName isEqualToString:upImageName]) {
        _upImageName = upImageName;
        if ([upImageName isUrlString]) {
            [self.UpIconButton sd_setBackgroundImageWithURL:[NSURL URLWithString:upImageName] forState:UIControlStateNormal];
        }else{
            [self.UpIconButton setBackgroundImage:[UIImage imageNamed:upImageName] forState:UIControlStateNormal];
        }
    }
}
-(void)setUpImageSelectName:(NSString *)UpImageSelectName{
    if (![_UpImageSelectName isEqualToString:UpImageSelectName]) {
        _UpImageSelectName = UpImageSelectName;
        if ([UpImageSelectName isUrlString]) {
            [self.UpIconButton sd_setBackgroundImageWithURL:[NSURL URLWithString:UpImageSelectName] forState:UIControlStateSelected];
        }else{
            [self.UpIconButton setBackgroundImage:[UIImage imageNamed:UpImageSelectName] forState:UIControlStateSelected];
        }
    }
}

-(void)setDownLableString:(NSString *)downLableString{
    if (![_downLableString isEqualToString:downLableString]) {
        _downLableString = downLableString;
        self.DownTitleLable.text = downLableString;
        [self.DownTitleLable sizeToFit];
    }
}
-(void)setCanClick:(BOOL)canClick{
    _canClick = canClick;
    if (canClick==YES) {
        self.tapGes.enabled = YES;
        self.UpIconButton.userInteractionEnabled = NO;
    }else{
        self.tapGes.enabled = NO;
        self.UpIconButton.userInteractionEnabled = YES;
    }
}
-(void)setIsSelect:(BOOL)isSelect{
    _isSelect = isSelect;
    [self changeStatusWith:isSelect];
}
#pragma mark ---排版---
-(void)layoutSubviews{
    [super layoutSubviews];
    __weak __typeof__(self) weakSelf = self;
    //配置
    if (self.imageSize.width==0) {
        [self.UpIconButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.mas_top).offset(weakSelf.marginImageTop==0?5:weakSelf.marginImageTop);
            make.centerX.equalTo(weakSelf.mas_centerX);
            make.size.mas_equalTo(CGSizeMake(15, 15));
        }];
    }else{
        [self.UpIconButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.mas_top).offset(weakSelf.marginImageTop==0?5:weakSelf.marginImageTop);
            make.centerX.equalTo(weakSelf.mas_centerX);
            make.size.mas_equalTo(weakSelf.imageSize);
        }];
    }
    self.DownTitleLable.font = self.downLableFont==nil?[UIFont systemFontOfSize:9.5]:self.downLableFont;
    
    
    if (![NSString isBlankStringWithString:self.downLableColorNomalString]&&![NSString isBlankStringWithString:self.downLableColorSelectStrintg] ) {
        if (self.isSelect) {
            self.DownTitleLable.textColor = [UIColor colorWithString:self.downLableColorSelectStrintg];
        }else{
            self.DownTitleLable.textColor = [UIColor colorWithString:self.downLableColorNomalString];
        }
    }else{
        self.DownTitleLable.textColor = [UIColor colorWithString:DownLableDefaultColor];
        
    }
    [self.DownTitleLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.mas_centerX);
        make.top.equalTo(weakSelf.UpIconButton.mas_bottom).offset(weakSelf.marginImageLable==0?5:weakSelf.marginImageLable);
    }];
    [self.DownTitleLable sizeToFit];
}

#pragma mark ---点击手势---
-(void)ClickSelfView{
    //    if (self.UpButton.selected == YES) {
    //        return;
    //    }else{
    //        [self changeStatusWith:YES];
    //    }
    if ([self.delegate respondsToSelector:@selector(clickTheViewFor:)]) {
        [self.delegate clickTheViewFor:self];
    }
}
-(void)changeStatusWith:(BOOL )isSelect{
    if (self.canClick) {
        if (isSelect) {
            self.UpIconButton.selected = YES;
            self.DownTitleLable.textColor = [UIColor colorWithString:self.downLableColorSelectStrintg];
        }else{
            self.UpIconButton.selected = NO;
            self.DownTitleLable.textColor = [UIColor colorWithString:self.downLableColorNomalString];
        }
    }
}

@end
